package cn.brilliantfuture.commentry.person.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.entity.DeliveryPerson;
import cn.brilliantfuture.commentry.person.DeliveryPersonMessageRemote;
import cn.brilliantfuture.commentry.person.DeliveryPersonMessageWebService;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2013-3-14
 * 
 * Description:
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.person.DeliveryPersonMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/DeliveryPersonMessageWebService")
public class DeliveryPersonMessageImpl implements DeliveryPersonMessageRemote, DeliveryPersonMessageWebService {
	
	private static Logger logger = Logger.getLogger(DeliveryPersonMessageImpl.class);
	
	@PersistenceContext(unitName = "commentry-jpa")
	EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public String[] getDeliveryPersonCards(String device) {
		logger.info("getDeliveryPersonCards(String device) device -> " + device);
		
		List<DeliveryPerson> deliveryPersons = entityManager.createQuery("select dp from DeliveryPerson dp inner join dp.lockerDeliveryPersons ldp where dp.deleted = false and ldp.lockers.device.deviceSn = '" + device + "'").getResultList();
		
		String[] personCards = null;
		
		if (null != deliveryPersons && deliveryPersons.size() > 0){
			personCards = new String[deliveryPersons.size()];
			
			for (int i = 0; i < personCards.length; i++) {
				personCards[i] = deliveryPersons.get(i).getNumber();
			}
		}
		
		return personCards;
	}

}
